

// 国家列表
$(function(){
    var lis = $("#ul_list li");
    var country = $(".country");
    var list_menu = $(".list_menu");
    var list = $(".list")
    list.click(function(){
        list_menu.css({
            display : "block"
        });
    })
    lis.click(function(){
        var index = $(this).html();
        country.html(index);
        list_menu.css({
            display : "none"
        });
    })
})

// 表单验证
    function Testing(){
        this.phonenum = $("#phonenum");
        this.bindEvent();
    }
    Testing.prototype.bindEvent = function(){
            var _this = this;
        this.phonenum.blur(function(){
            this.tip = $("#name-tip");
            var all_num = /^1\d{10}/;
            var user_value = this.value;
            if(user_value === ""){
                validate_result[0].pass = false;
                this.tip.css({
                    display : "block"
                });
                this.tip.html("请输入手机号码");
                _this.phonenum.css({
                    borderColor : "red"
                });
            }else if(!all_num.test(user_value)){
                validate_result[0].pass = false;
                this.tip.css({
                    display : "block"
                });
                this.tip.html("手机号码格式错误");
                _this.phonenum.css({
                    borderColor : "red"
                });
            }else if(all_num.test(user_value)){
                validate_result[0].pass = true;
                this.tip.css({
                    display : "none"
                });
                _this.phonenum.css({
                    borderColor : "#" + "ddd"
                });
            }
        })
    }
    new Testing();

// 生成验证码
    var ver_type = false;
    var ver_btn = $(".ver-btn");
    var ver_code = 0;
    $(ver_btn).click(function(){
        ver_code = parseInt( Math.random() * 10000 );
        if((ver_code / 10000) > 0.1){
            alert("您的验证码为 : " + ver_code);
        }
    })
    
    var ver_input = $("#ver-code");
    $(ver_input).blur(function(){
        var tip = $("#ver-tip");
        var ver_value =Number(this.value);

        if(ver_value !== ver_code){
            ver_type = false;
            validate_result[1].pass = false;
            tip.css({
                display : "block"
            });
            ver_input.css({
                borderColor : "red"
            });
            tip.html("验证码错误");
        }else{
            ver_type = true;
            validate_result[1].pass = true;
            tip.css({
                display : "none"
            });
            ver_input.css({
                borderColor : "#" + "ddd"
            });
        }
    });

    // 密码验证
    var psw_type = false;
    var psw_input = $("#password");
    $(psw_input).on("input" , function(){
        validate_result[2].pass = false;
        var tip = $("#psw-tip");
        var psw_value = this.value;
        if(psw_value.length < 8){
            psw_type = false;
            tip.css({
                display : "block",
                color : "#" + 333
            });
            tip.html("密码为8-20为字母和数字或者特殊符号组合");
            return false;
        }
        var strength = 0;
    if(/\d+/.test(psw_value)){
        strength ++;
    }
    if(/[a-z]/.test(psw_value)){
        strength ++;
    }
    if(/[\!\@\#\$\%\^\&\*\(\)\_\+\-]/.test(psw_value)){
        strength ++;
    }
    switch(strength){
        case 1 :
            tip.css({
                
                display : "block",
                color : "red"
            });
            tip.html("密码过于简单，有被盗风险，建议您更改为复杂密码");
            break;
        case 2 :
            tip.css({
                display : "block",
                color : "orange"
            })
            tip.html("密码安全强度适中");
            break;
        case 3 :
            tip.css({
                display : "block",
                color : "green"
            })
            tip.html("你的密码很安全");
            break;
    }
})

$(psw_input).blur(function(){
    var tip = $("#psw-tip");
        var psw_value = this.value;
        
    if(psw_value.length >= 8 && psw_value.length <= 20){
        psw_type = true;
        validate_result[2].pass = true;
        tip.css({
            display : "none",
            color : "#" + 333
        });
        tip.html("密码为8-20为字母和数字或者特殊符号组合");
    }else if(psw_value === ""){
        psw_type = false;
        tip.css({
            display : "block",
            color : "red"
        });
        tip.html("密码不能为空");
    }
})

var btn = document.getElementById("btn");
// 创建一个存储表单验证状态的变量; 
var validate_result = [
    {
      ele : phonenum,
      pass : false
    },
    {
        ele : ver_input,
        pass : false
    },
    {
        ele : psw_input,
        pass : false
    }
  ]
  btn.onclick = function(){
    // 遍历表单验证列表;
    for(var i = 0 ; i < validate_result.length ; i ++){
      // 查看是不是有没有通过验证的元素，如果有就终止程序执行; 
      if( !validate_result[i].pass ){
        validate_result[i].ele.focus();
        return false;
        // 应该终止程序执行; 
      }
    }
}



// ajax 操作

$(function(){
    var phonenum_ipt = $("#phonenum");
    var password_ipt = $("#password");
    var btn = $("#btn");
    
    btn.click(function(){
        var phonenum_value = phonenum_ipt.val();
        var password_value = password_ipt.val();

        if(ver_type && psw_type){
            $.ajax({
                url : "http://localhost/server/sign.php",
                dataType : "json",
                data :({
                    phonenum : phonenum_value,
                    password : password_value,
                    type : "register"
                })
            })
            .then(function(data){
                if(data.type === "success"){
                    alert("注册成功");
                    location.href = "index.html"
                    cookie("phonenum" , phonenum_value);
                    cookie("password" , password_value);
                }else{
                    alert("该手机号已被占用，换一个试试");
                }
            })
        }
    })
})